package com.kyzh.core.adapters


import android.graphics.Color
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.ui.ArcButton
import com.gushenge.core.beans.Game
import com.gushenge.core.beans.Text
import com.gushenge.core.impls.SearchImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.pager.home.homesearch.SearchArrActivity
import com.kyzh.core.utils.startActivity
import com.kyzh.core.utils.startGameDetailActivity
import com.gushenge.core.toast
import org.litepal.LitePal

class SearchHistoryAdapter(layoutId: Int, val beans: MutableList<Text>, val type: Boolean) : BaseQuickAdapter<Text, BaseViewHolder>(layoutId, beans) {

    override fun convert(helper: BaseViewHolder, item: Text) {
        val text = helper.getView<ArcButton>(R.id.text)
        text.text = item.name
        if (!type) {
            when (helper.layoutPosition % 4) {
                0 -> {
                    text.setBackgroundColor(Color.rgb(251, 235, 236))
                    text.setTextColor(Color.rgb(245, 67, 83))
                }
                1 -> {
                    text.setBackgroundColor(Color.rgb(251, 245, 223))
                    text.setTextColor(Color.rgb(255, 133, 35))
                }
                2 -> {
                    text.setBackgroundColor(Color.rgb(233, 244, 238))
                    text.setTextColor(Color.rgb(146, 205, 67))
                }
                3 -> {
                    text.setBackgroundColor(Color.rgb(238, 241, 248))
                    text.setTextColor(Color.rgb(173, 200, 253))
                }
            }
        } else {
            text.setBackgroundColor(Color.rgb(245, 245, 245))
            text.setTextColor(Color.rgb(102, 102, 97))
        }

        text.setOnClickListener {v->
            LitePal.deleteAll(Text::class.java, "name=?", item.name)
            Text(item.name).save()
            SearchImpl().search(item.name, object : ResultListener {
                override fun success(bean: Any) {
                    bean as ArrayList<Game>
                    if (bean.size==1){
                        context.startGameDetailActivity(bean[0].gid)
                    }else{
                        context.startActivity<SearchArrActivity>("text" to item.name)
                    }
                }

                override fun error(error: String) {
                    error.toast()
                }
            })

        }

    }


}